package com.picket.dao;

import java.util.ArrayList;
import java.util.Collection;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.springframework.orm.jdo.support.JdoDaoSupport;
import org.springframework.stereotype.Repository;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.picket.domain.UserPool;

@Repository
public class UserPoolDao extends JdoDaoSupport implements IUserPoolDao {

	@Override
	public UserPool create(UserPool userPool) {
		getJdoTemplate().makePersistent(userPool);
		System.out.println(userPool.getKey());
		return userPool;

	}

	@Override
	public Collection<UserPool> getAllUserPools() {

		final PersistenceManager pm = getPersistenceManager();

		try {

			final Query query = pm.newQuery(UserPool.class);

			@SuppressWarnings("unchecked")
			final Collection<UserPool> userpools = (Collection<UserPool>) query
					.execute();

			return pm.detachCopyAll((Collection<UserPool>) userpools);
		}

		finally {
			releasePersistenceManager(pm);
		}

	}

	@Override
	public Collection<UserPool> getAllUserPoolsForEvent(long eventId) {

		final PersistenceManager pm = getPersistenceManager();

		try {

			final Query query = pm.newQuery(UserPool.class);
			query.setFilter("eventId == eventIdParam");
			query.declareParameters("long eventIdParam");
			@SuppressWarnings("unchecked")
			final Collection<UserPool> userpools = (Collection<UserPool>) query
					.execute(eventId);

			return pm.detachCopyAll((Collection<UserPool>) userpools);
		}

		finally {
			releasePersistenceManager(pm);
		}

	}

	@Override
	public Collection<UserPool> getAllUserPoolsForPool(long poolId) {
		final PersistenceManager pm = getPersistenceManager();

		try {

			final Query query = pm.newQuery(UserPool.class);
			query.setFilter("poolId == poolIdParam");
			query.setOrdering("userEmail ascending");
			query.declareParameters("long poolIdParam");
			
			@SuppressWarnings("unchecked")
			final Collection<UserPool> userpools = (Collection<UserPool>) query
					.execute(poolId);

			return pm.detachCopyAll((Collection<UserPool>) userpools);
		}

		finally {
			releasePersistenceManager(pm);
		}
	}

	@Override
	public UserPool getUserPool(long id) {

		final PersistenceManager pm = getPersistenceManager();

		try {

			Key k = KeyFactory.createKey(UserPool.class.getSimpleName(), id);
			return pm.getObjectById(UserPool.class, k);

		}

		finally {
			releasePersistenceManager(pm);
		}

	}

	@Override
	public Collection<UserPool> findByEmail(String email) {

		final PersistenceManager pm = getPersistenceManager();

		try {

			final Query query = pm.newQuery(UserPool.class);
			query.setFilter("userEmail == userEmailParam");
			query.declareParameters("String userEmailParam");
			@SuppressWarnings("unchecked")
			final Collection<UserPool> userpools = (Collection<UserPool>) query
					.execute(email);

			return pm.detachCopyAll((Collection<UserPool>) userpools);
		}

		finally {
			releasePersistenceManager(pm);
		}

	}

	@Override
	public void updatePoints(Long id, Integer totalPoints, Integer totalCorrect) {
		PersistenceManager pm = getPersistenceManager();
		UserPool userPool = null;

		try {
			pm.currentTransaction().begin();
			// We don't have a reference to the selected Product.
			// So we have to look it up first,
			Key k = KeyFactory.createKey(UserPool.class.getSimpleName(), id);
			userPool = pm.getObjectById(UserPool.class, k);

			userPool.setPointsScored(totalPoints);
			userPool.setCorrectPredictions(totalCorrect);

			pm.makePersistent(userPool);
			pm.currentTransaction().commit();
		} catch (Exception ex) {
			pm.currentTransaction().rollback();
			throw new RuntimeException(ex);
		} finally {
			pm.close();
		}
	}
	
	
	@Override
	public UserPool findByPoolId(long poolId){
		final PersistenceManager pm = getPersistenceManager();
		try {
			final Query query = pm.newQuery(UserPool.class);
			query.setFilter("poolId == poolIdParam");
			query.declareParameters("long poolIdParam");
			//final UserPool userpool = (UserPool) query.execute(poolId);
			@SuppressWarnings("unchecked")
			Collection<UserPool> userpools = (Collection<UserPool>) query
					.execute(poolId);

			Object[] upools= (pm.detachCopyAll((Collection<UserPool>) userpools)).toArray();
			if(upools.length == 0)
				return null;
			else
				return (UserPool)upools[0] ;
		}

		finally {
			releasePersistenceManager(pm);
		}
	}	
	
	@Override
	public Collection<UserPool> findByEmailAndPoolId(String email,long poolId){
		final PersistenceManager pm = getPersistenceManager();
		try {
			final Query query = pm.newQuery(UserPool.class);
			query.setFilter("poolId == poolIdParam");
			//query.setFilter("userEmail == emailParam");
			query.declareParameters("long poolIdParam");
			//query.declareParameters("long poolIdParam,String emailParam");
			
			
			//final UserPool userpool = (UserPool) query.execute(poolId);
			@SuppressWarnings("unchecked")
			Collection<UserPool> userpools = (Collection<UserPool>) query
					.execute(poolId);
			Collection<UserPool> userpools2 = new ArrayList<UserPool>();
			for(UserPool userpool:userpools){
				if(userpool.getUserEmail().equalsIgnoreCase(email)){
					userpools2.add(userpool);	
				}
				
			}
			pm.detachCopyAll((Collection<UserPool>) userpools);
			return userpools2;
		}

		finally {
			releasePersistenceManager(pm);
		}
	}
	

}